package com.mobile.service.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.mobile.service.dto.base.BaseDTO;
import com.mobile.service.dto.base.PageDTO;
import com.mobile.service.dto.base.StringListDeserializer;
import com.mobile.util.PageData;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

// Role DTO
@EqualsAndHashCode(callSuper = true)
@Data
public class RoleDTO extends BaseDTO {
    @Schema(description = "角色ID", accessMode = Schema.AccessMode.READ_ONLY)
    private Integer roleId;
    @Schema(description = "角色代码")
    private String roleCode;
    @Schema(description = "角色名称")
    private String roleName;
    @Schema(description = "管理员权限")
    private String administratorRights;
    // getter
    @Getter
    @Schema(description = "权限集合", accessMode = Schema.AccessMode.WRITE_ONLY ,example = " [1-1,1-2,1-3,1-4]" )
    private List<String> auths;
    @Schema(description = "权限集合", accessMode = Schema.AccessMode.READ_ONLY ,example = " [1-1,1-2,1-3,1-4]" )
    private  List<PageData> treeData;


    // 自定义setter处理数据
    public void setAuths(List<String> auths) {
        if (auths != null && !auths.isEmpty() && auths.get(0).startsWith("[")) {
            // 处理格式异常的数组
            this.auths = auths.stream()
                    .flatMap(s -> Arrays.stream(s.split(",")))
                    .map(item -> item.replaceAll("[\\[\\]\"]", "").trim())
                    .filter(item -> !item.isEmpty())
                    .collect(Collectors.toList());
        } else {
            this.auths = auths;
        }
    }

}

